<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
      class="globalSerchFormStyle"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="手机号" prop="icCardname">
            <el-input
              v-model="queryParams.icContactsphone"
              placeholder="请输入手机号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 190px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="卡号" prop="icCardno">
            <el-input
              v-model="queryParams.icCardno"
              placeholder="请输入卡号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 190px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="卡类型" prop="cardType">
            <el-select
              v-model="queryParams.cardType"
              placeholder="请选择卡类型"
              clearable
              size="small"
              style="width: 190px"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="卡名称" prop="icCardname">
            <el-input
              v-model="queryParams.icCardname"
              placeholder="请输入卡名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 190px"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="collapse">
        <el-col :span="6">
          <el-form-item label="联系人" prop="icContacts">
            <el-input
              v-model="queryParams.icContacts"
              placeholder="请输入联系人"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
              style="width: 190px"
            />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="6">
          <el-form-item label="创建日期" prop="createDate">
            <el-date-picker
              clearable
              size="small"
              style="width: 190px"
              v-model="queryParams.createDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择创建日期"
            >
            </el-date-picker>
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
    <el-row class="global_el_rowStyle" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          type="primary"
          plain
          >重置</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          size="mini"
          id="closeSearchBtn"
          @click="
            (closeSearch) => {
              collapse = !collapse;
            }
          "
        >
          {{ collapse ? "收起" : "展开更多" }}
          <i :class="collapse ? 'el-icon-arrow-up ' : 'el-icon-arrow-down'"></i>
        </el-button>
      </el-col>
    </el-row>
    <!-- <el-row :gutter="10" class="mb8">
    <el-table
        :data="[{}]"
        :border='true'
        style="width: 100%">
      <af-table-column
          label="汇总">
        合计开卡人数：<b>{{ statistics.totalNumber}}</b>
      </af-table-column>
      <af-table-column>
        累计充值总额：<b>{{ statistics.totalrecharge }}</b>
      </af-table-column>
      <af-table-column>
        <template slot="header">
          <i class="el-icon-folder-opened"></i>
        </template>
        <template>
          当前所有用户账户余额总计:<b>{{ statistics.totalamt  }}</b>
        </template>
      </af-table-column>
    </el-table>
  </el-row> -->
    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
      plain
    >
      <af-table-column type="selection" width="55" align="left" />
      <af-table-column
        label="卡类别"
        align="left"
        prop="cardType"
        :formatter="changecardTypeProp"
        sortable
      />
      <af-table-column label="卡号" align="left" prop="icCardno" sortable />
      <af-table-column label="卡名称" align="left" prop="icCardname" sortable />
      <af-table-column
        label="手机号"
        align="left"
        prop="icContactsphone"
        sortable
      />
      <!-- <af-table-column label="充值总额" align="left" prop="totalrecharge"  width="100"/> -->
      <af-table-column label="余额" align="left" prop="amt" sortable />
      <af-table-column label="联系人" align="left" prop="icContacts" sortable />
      <!-- <af-table-column label="是否启用" align="left" prop="enable" /> -->
      <!-- <af-table-column label="免密支付" align="left" width="100" prop="issecret" :formatter="changeissecretProp" /> -->
      <af-table-column label="开卡时间" align="left" prop="createDate" sortable>
        <template slot-scope="scope">
          <span>{{ scope.row.createDate }}</span>
        </template>
      </af-table-column>
      <af-table-column
        label="操作"
        align="left"
        class-name="small-padding"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-sugar"
            @click="handlePay(scope.row)"
            >充值</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleConsumption(scope.row)"
            >消费</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handlecancellation(scope.row)"
            >注销</el-button
          >
        </template>
      </af-table-column>
    </el-table>

    <div class="pagination_center">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改卡编号对话框 -->
    <el-dialog
      title="实体卡充值"
      :visible.sync="openPay"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="金额" prop="amt">
          <el-input v-model="form.amt" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="油站" prop="amt">
          <!-- <el-input v-model="form.amt" placeholder="请输入充值金额" /> -->
          <el-select
            v-model="form.stationId"
            placeholder="请选择油站"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in stationList"
              :key="index"
              :label="item.stationName"
              :value="item.stationId"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
          <div>油站选项只记名，余额为油企下的油站通用</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="funPay">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户消费对话框 -->
    <el-dialog
      title="实体卡扣款"
      :visible.sync="openConsumption"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="金额" prop="amt">
          <el-input v-model="form.amt" placeholder="请输入消费金额" />
        </el-form-item>
        <el-form-item label="油站" prop="amt">
          <!-- <el-input v-model="form.amt" placeholder="请输入充值金额" /> -->
          <el-select
            v-model="form.stationId"
            placeholder="请选择油站"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in stationList"
              :key="index"
              :label="item.stationName"
              :value="item.stationId"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
          <div>油站选项只记名，余额为油企下的油站通用</div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="funConsumption">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 重置密码对话框 -->
    <el-dialog :title="title" :visible.sync="openPasswd" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="新密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入新密码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="funUpdatePwd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 注销ic卡 -->
    <el-dialog :title="title" :visible.sync="opencancellation" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="注销原因" prop="password">
          <el-input v-model="form.cancellation" placeholder="请输入注销原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="funcancellation">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  updatepwd,
  statisticsInfo,
  updateRecharge,
  updateConsumption,
  updatecancellation,
  exportInfo,
} from "@/api/ic/info";
import { getStationList } from "@/api/dept";

export default {
  name: "Info",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 卡编号表格数据
      infoList: [],
      statistics: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      openPay: false,
      openConsumption: false,
      // 是否弹出充值密码
      openPasswd: false,
      //是否弹出注销
      opencancellation: false,
      options: [
        {
          value: "1",
          label: "汽油",
        },
        {
          value: "2",
          label: "柴油",
        },
        {
          value: "3",
          label: "LNG",
        },
        {
          value: "4",
          label: "CNG",
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        icCardno: null,
        icContactsphone: null,
        cardType: null,
        icCardname: null,
        createDate: null,
        icContacts: null,
        password: null,
        amt: null,
        enable: 0,
        issecret: null,
        cancellation: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      stationList: [],
      collapse: false,
    };
  },
  created() {
    this.getList();
    this.getStationList();
  },
  methods: {
    getStationList() {
      getStationList({
        enterpriseId: this.presentEnterprise.enterpriseId,
      })
        .then((res) => {
          this.stationList = res.rows;
        })
        .finally();
    },
    //改变显示的人物属性
    changecardTypeProp(row) {
      const manProp = row.cardType;
      var carTypeMsg = "通用";
      if (manProp == "1") {
        carTypeMsg = "汽油";
      } else if (manProp == "2") {
        carTypeMsg = "柴油";
      } else if (manProp == "3") {
        carTypeMsg = "LNG";
      } else if (manProp == "4") {
        carTypeMsg = "CNG";
      }
      return carTypeMsg;
    },
    changeissecretProp(row) {
      const manProp = row.issecret;
      var issecretMsg = "否";
      if (manProp == "1") {
        issecretMsg = "是";
      }
      return issecretMsg;
    },
    /** 查询卡编号列表 */
    getList() {
      // this.getstatisticsInfo()
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getstatisticsInfo() {
      statisticsInfo(this.queryParams).then((response) => {
        this.statistics = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.openPay = false;
      this.openPasswd = false;
      this.opencancellation = false;
      this.openConsumption = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        icCardno: null,
        icOldcardno: null,
        icContactsphone: null,
        cardType: null,
        icCardname: null,
        createDate: null,
        icContacts: null,
        password: null,
        amt: null,
        enable: null,
        issecret: null,
        cancellation: null,
        rechargeSource: 1,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.icCardno);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 注销实体操作 */
    handlecancellation(row) {
      const icCardnos = row.icCardno || this.ids;
      this.form.icCardno = icCardnos;
      const that = this;
      this.$confirm(
        '是否确认删除卡编号编号为"' + icCardnos + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(function () {
        that.opencancellation = true;
      });
    },
    funcancellation() {
      updatecancellation(this.form).then((response) => {
        if (response.code == 200) {
          this.msgSuccess("注销成功");
          this.getList();
          this.reset();
          this.opencancellation = false;
        } else {
          this.msgSuccess(response.msg);
        }
      });
    },
    /** 重置密码操作 */
    handleRestPasswd(row) {
      const icCardnos = row.icCardno || this.ids;
      this.form.icCardno = icCardnos;
      const that = this;
      this.$confirm(
        '是否确认重置卡编号编号为"' + icCardnos + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(function () {
        that.openPasswd = true;
      });
    },
    funUpdatePwd() {
      updatepwd(this.form).then((response) => {
        if (response.code == 200) {
          this.msgSuccess("重置成功");
          this.reset();
          this.openPasswd = false;
        } else {
          this.msgSuccess(response.msg);
        }
      });
    },
    /** 充值对话框 */
    handlePay(row) {
      const icCardnos = row.icCardno || this.ids;
      this.form.icCardno = icCardnos;
      const that = this;
      this.$confirm('是否确认为卡编号为"' + icCardnos + '"充值?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        that.openPay = true;
      });
    },
    handleConsumption(row) {
      const icCardnos = row.icCardno || this.ids;
      this.form.icCardno = icCardnos;
      const that = this;
      this.$confirm('是否确认为卡编号为"' + icCardnos + '"扣款?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        that.openConsumption = true;
      });
    },
    funPay() {
      updateRecharge(this.form).then((response) => {
        if (response.code == 200) {
          this.msgSuccess("充值成功");
          this.reset();
          this.openPay = false;
          this.getList();
        } else {
          this.msgSuccess(response.msg);
        }
      });
    },
    funConsumption() {
      updateConsumption(this.form).then((response) => {
        if (response.code == 200) {
          this.msgSuccess("扣款成功");
          this.reset();
          this.openConsumption = false;
          this.getList();
        } else {
          this.msgSuccess(response.msg);
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有卡编号数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportInfo(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
